//
//  NEDMediatorNetwork.m
//  magicBox
//
//  Created by 98du on 2021/12/16.
//

#import "NEDMediatorNetwork.h"
#import <NEDNetwork/NEDRequest.h>
#import <NEDNetwork/NEDRequestResponse.h>

/// 请求的错误码
typedef NS_ENUM(NSInteger, NEDErrorCode) {
    NEDErrorCode_UnKnow = -1,
    NEDErrorCode_Success = 0,/// 成功
    NEDErrorCode_Failure = 1,/// 失败
    
    
    NEDErrorCode_TokenExpire = 401,/// Token失效
    
};

@implementation NEDMediatorNetwork


- (NSString *)http_defaultDomain{
    return @"";
}

- (void)http_handleErrorCode:(NSInteger)errorCode
          responseStatusCode:(NSInteger)responseStatusCode
                     message:(NSString *)message
                     request:(NEDRequest *)request{
    if(errorCode == NEDErrorCode_Failure){
        if(responseStatusCode == NEDErrorCode_TokenExpire){
            [self __headleTokenExpire];
            return;
        }
    }
    
    if(message && message.length > 0){
        NSLog(@"显示错误提示%@",message);
    }
}

- (NSString*)http_codeStr:(NSInteger)code{
    NSString * scode = @"";
    switch (code) {
        case 202: { scode = @"正紧急通知服务器处理您的请求!" ;} break;
        case 400: { scode = @"参数有误，您的操作姿势有待改进哦!" ;} break;
        case 401: { scode = @"不好意思，当前用户无法验证!" ;} break;
        case 403: { scode = @"太频繁啦，您先歇息一下!" ;} break;
        case 404: { scode = @"您的请求失败，无法连接到服务器!" ;} break;
        case 408: { scode = @"哎呀，超时啦!" ;} break;
        case 409: { scode = @"请求内容和请求资源存在冲突!" ;} break;
        case 410: { scode = @"请求资源不可用!" ;} break;
        case 413: { scode = @"您的文件太大,请压缩后再试!" ;} break;
        case 414: { scode = @"请求链接过长，服务器无法处理!" ;} break;
        case 415: { scode = @"请求格式不支持，服务器无法处理!" ;} break;
        case 421: { scode = @"服务器支持请求数量已达上限!" ; } break;
        case 422: { scode = @"请求格式正确，但语义错误，无法响应!" ;} break;
        case 500: { scode = @"服务器繁忙，请稍后再试!" ;} break;
        case 501: { scode = @"服务器不支持当前请求!" ;} break;
        case 502: { scode = @"服务器未响应，请稍后再试!" ;} break;
        case 503: { scode = @"服务器内部繁忙，请稍后再试!" ;} break;
        case 504: { scode = @"哎呀，请求服务器超时啦!" ;} break;
        case 505: { scode = @"服务器不支持当前请求!" ;} break;
        case 507: { scode = @"服务器努力过，但依然无法储存当前的数据!" ;} break;
            
        default: break;
    }
    return scode;
}

/// 请求头公用配置
- (NSDictionary <NSString *,NSString *>*)http_requestHeaderFieldValuePublic{
    NSMutableDictionary * fieldValue = [NSMutableDictionary new];
    fieldValue[@"Content-Type"] = @"application/json; charset=utf-8";
    fieldValue[@"app_version"] = @"1.0.0";
    fieldValue[@"Accept-Language"] = @"zh";
    fieldValue[@"Platform"] = @"iOS";
    fieldValue[@"ApiVersion"] = @"1.0.0";
    BOOL logined = YES;
    if(logined){
        fieldValue[@"Authorization"] = @"Bearer tokentokentokentokentokentokentokentokentokentoken";
    }
    return fieldValue;
}

/// 开始请求
- (void)http_requestStart:(NEDRequest *)request{
    if(request.config.showLoading){
        NSLog(@"显示加载提示框");
    }
}
/// 结束请求
- (void)http_requestEnd:(NEDRequest *)request{
    if(request.config.showLoading){
        NSLog(@"隐藏加载提示框");
    }
}
/// 请求成功
- (void)http_requestSuccess:(NEDRequest *)request response:(NEDRequestResponse *)response{
    
}
/// 请求失败
- (void)http_requestFail:(NEDRequest *)request  response:(NEDRequestResponse *)response{
    
}


#pragma mark - privite

- (void)__headleTokenExpire{
    NSLog(@"token 过期了");
}
@end
